package org.tlipoca.smsteamcategory.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.constant.SteamApiURLConstant;
import org.genntii.smcommon.constant.SteamConstant;
import org.genntii.smcommon.utils.HttpClientUtil;
import org.genntii.smdomain.entity.SteamCategory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.tlipoca.smsteamcategory.mapper.SteamCategoryMapper;

import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class SteamCategoryTask {

    @Resource
    private SteamCategoryMapper steamCategoryMapper;

    // SpringTask 每日04:00 自动执行
//    @Scheduled(cron = " 0 0 4 * * ? ")
    public void getSteamCategoryList() {
        JSONObject result = null;
        Map<String, Object> map = Map.of("language", SteamConstant.STEAM_CHINESE_LANGUAGE, "key", SteamConstant.STEAM_KEY);

        result = (JSONObject) com.alibaba.fastjson.JSON.parseObject(HttpClientUtil.doGetWithRetry(SteamApiURLConstant.GET_CATEGORY, map, 50)).get("response");
        log.info("获取Steam分类数据成功");

        log.info("更新Mysql Steam标签");
        List<SteamCategory> categories = JSON.parseArray(result.get("categories").toString(), SteamCategory.class);
        steamCategoryMapper.deleteAll();
        steamCategoryMapper.insertBatch(categories);
        log.info("更新Mysql Steam标签完成");
    }

}
